package tw.asap.plus.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.response.ShopIndex;
import tw.asap.plus.ui.AsapBaseActivity;
import tw.asap.plus.ui.adapter.MerchantListAdapter;
import tw.asap.plus.ui.custom.AppBar;

/**
 * Created by Yax on 15/11/9.
 */
public class CouponActivity extends AsapBaseActivity {

    @InjectView(R.id.appbar)
    AppBar appbar;
    @InjectView(R.id.recycler_view)
    RecyclerView recyclerView;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @InjectView(R.id.extra_layout)
    FrameLayout extraLayout;
    private MerchantListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_collection);
        ButterKnife.inject(this);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.setColorSchemeResources(R.color.palette_red);
            swipeRefreshLayout.setOnRefreshListener(() ->
                    swipeRefreshLayout.postDelayed(() -> swipeRefreshLayout.setRefreshing(false), 1500l));
        }
        appbar.setLeftClickEvent(v -> CouponActivity.this.finish());
        appbar.setTitle(getResources().getString(R.string.my_coupon));

        displayExtraLayout(1);
    }

    private void initRecyclerView(List<ShopIndex.ShopEntity> list) {
        extraLayout.setVisibility(View.GONE);
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        adapter = new MerchantListAdapter(this, list);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);
    }


    private void displayExtraLayout(int type) {
        if (extraLayout.getChildCount() > 0) {
            extraLayout.removeAllViews();
        }
        extraLayout.setVisibility(View.VISIBLE);
        switch (type) {
            case 0://loading
                swipeRefreshLayout.setVisibility(View.GONE);
                LayoutInflater.from(this).inflate(R.layout.layout_loading, extraLayout);
                break;
            case 1://noData
                swipeRefreshLayout.setVisibility(View.GONE);
                View emptyView = LayoutInflater.from(this).inflate(R.layout.layout_no_collection, extraLayout);
                ((ImageView) emptyView.findViewById(R.id.icon)).setImageResource(R.drawable.ic_no_coupon);
                ((TextView) emptyView.findViewById(R.id.empty_hint)).setText(R.string.no_coupon);
                break;

            case 2://noNetwork
                swipeRefreshLayout.setVisibility(View.GONE);
                LayoutInflater.from(this).inflate(R.layout.layout_no_collection, extraLayout);
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (swipeRefreshLayout != null) {
            Log.d("onPause", "swipeRefreshLayout！＝null");
            swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.destroyDrawingCache();
            swipeRefreshLayout.clearAnimation();
        }
    }
}
